package main

import "fmt"

type data int

func (d data) String() string  {
	return fmt.Sprintf("data:%d",d)
}

type FuncString func() string

func (f FuncString) String() string  {
	return f()
}
/*
type z int
func init() {
	var _ fmt.Stringer = z(0)
}
*/
func main() {
	var d data = 15
	var x interface{} =d
	//转会为更具体的接口类型
	if n,ok := x.(fmt.Stringer); ok{
		fmt.Println(n)
	}
	//转换回原生类型
	if d2,ok := x.(data);ok {
		fmt.Println(d2)
	}
	//main.data is not error: missing method Error
	//e := x.(error)
	//fmt.Println(e)

	//使用ok-idion模式，即便转换失败也不会ying'fa
	var m interface{} = func(x int) string {
		return fmt.Sprintf("d:%d",x)
	}
	switch v:= m.(type){
	case nil:
		println("nil")
	case *int:
		println(*v)
	case func(int) string:
		println(v(100))
	case fmt.Stringer:
		println(v)
	default:
		println("unknown")
	}


	var t fmt.Stringer = FuncString(func() string {
		return "hello world"
	})

	fmt.Println(t)
}
